// 其实文字处理，真的要有好多东西要学，这 TTF 库应该只是冰山一角
#include "xege.h"
#include "font.h"
// https://www.libsdl.org/projects/SDL_ttf/docs/SDL_ttf.html
namespace xege {
	// UNICODE 转 UTF-8 编码(but to be honest, 2003 some organization limited the lenth of utf8 to 4 byte)
	//                          uncide 代码           存储 utf8 编码的字符串  字符串大小
	int FONT::unicode2utf8(unsigned long unic, unsigned char *pOutput, int outSize) {
		// assert(pOutput != NULL);
		// assert(outSize >= 6);
			// * U-00000000 - U-0000007F:       0xxxxxxx
			// * U-00000080 - U-000007FF:       110xxxxx 10xxxxxx
			// * U-00000800 - U-0000FFFF:       1110xxxx 10xxxxxx 10xxxxxx
			// * U-00010000 - U-001FFFFF:       11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
			// * U-00200000 - U-03FFFFFF:       111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
			// * U-04000000 - U-7FFFFFFF:       1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx

		if (unic <= 0x0000007F) {
			// * U-00000000 - U-0000007F:  0xxxxxxx
			*pOutput     = (unic & 0x7F);
			return 1;
		} else if (unic >= 0x00000080 && unic <= 0x000007FF) {
			// * U-00000080 - U-000007FF:  110xxxxx 10xxxxxx
			*(pOutput+1) = (unic & 0x3F) | 0x80;
			*pOutput     = ((unic >> 6) & 0x1F) | 0xC0;
			return 2;
		} else if (unic >= 0x00000800 && unic <= 0x0000FFFF) {
			// * U-00000800 - U-0000FFFF:  1110xxxx 10xxxxxx 10xxxxxx
			*(pOutput+2) = (unic & 0x3F) | 0x80;
			*(pOutput+1) = ((unic >>  6) & 0x3F) | 0x80;
			*pOutput     = ((unic >> 12) & 0x0F) | 0xE0;
			return 3;
		} else if (unic >= 0x00010000 && unic <= 0x001FFFFF) {
			// * U-00010000 - U-001FFFFF:  11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
			*(pOutput+3) = (unic & 0x3F) | 0x80;
			*(pOutput+2) = ((unic >>  6) & 0x3F) | 0x80;
			*(pOutput+1) = ((unic >> 12) & 0x3F) | 0x80;
			*pOutput     = ((unic >> 18) & 0x07) | 0xF0;
			return 4;
		} else if (unic >= 0x00200000 && unic <= 0x03FFFFFF) {
			// * U-00200000 - U-03FFFFFF:  111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
			*(pOutput+4) = (unic & 0x3F) | 0x80;
			*(pOutput+3) = ((unic >>  6) & 0x3F) | 0x80;
			*(pOutput+2) = ((unic >> 12) & 0x3F) | 0x80;
			*(pOutput+1) = ((unic >> 18) & 0x3F) | 0x80;
			*pOutput     = ((unic >> 24) & 0x03) | 0xF8;
			return 5;
		} else if (unic >= 0x04000000 && unic <= 0x7FFFFFFF) {
			// * U-04000000 - U-7FFFFFFF:  1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
			*(pOutput+5) = (unic & 0x3F) | 0x80;
			*(pOutput+4) = ((unic >>  6) & 0x3F) | 0x80;
			*(pOutput+3) = ((unic >> 12) & 0x3F) | 0x80;
			*(pOutput+2) = ((unic >> 18) & 0x3F) | 0x80;
			*(pOutput+1) = ((unic >> 24) & 0x3F) | 0x80;
			*pOutput     = ((unic >> 30) & 0x01) | 0xFC;
			return 6;
		}
		return 0;
	}
	// 加载字体，暂不支持从系统中加载
	void FONT::loadFont (std::string filepath, int size) {
		font = TTF_OpenFont (filepath.c_str (), size);
		// SDL_Log ("load Font");
		if (!font) {
			// std::cerr << "TTF_OpenFont error:" << TTF_GetError ();
			SDL_Log ("\nTTF_OpenFont error: %s\n", TTF_GetError ());
			SDL_Quit ();
		}
	}

	SDL_Surface* FONT::renderFontRF (std::string text, color::RGBA F) {
		return TTF_RenderUTF8_Solid (font, text.c_str (), F);
	}
	SDL_Surface* FONT::renderFontRF (unsigned char* text,	color::RGBA F) {
		return TTF_RenderUTF8_Solid (font, (char*)text, F);
	}
	SDL_Surface* FONT::renderFontF (std::string text,	color::RGBA F, color::RGBA B) {
		return TTF_RenderUTF8_Shaded (font, text.c_str (), F, B);
	}
	SDL_Surface* FONT::renderFontF (unsigned char* text, color::RGBA F, color::RGBA B) {
		return TTF_RenderUTF8_Shaded (font, (char*)text, F, B);
	}
	SDL_Surface* FONT::renderFont (std::string text, color::RGBA F) {
		return TTF_RenderUTF8_Blended (font, text.c_str (), F);
	}
	SDL_Surface* FONT::renderFont (unsigned char* text, color::RGBA F) {
		return TTF_RenderUTF8_Blended (font, (char*)text, F);
	}

	FONT::FONT (std::string filepath, int size) {
		if (!TTF_WasInit() && TTF_Init () == -1) {
			SDL_Log ("\nTTF_Init error: %s", TTF_GetError ());
			SDL_Quit ();
		}
		loadFont (filepath, size);
	}

	FONT::~FONT () {
		TTF_CloseFont (font);
		font = nullptr;
		// TTF_Quit ();
	}

}  // 命名空间 xege 结尾